/*
 * Copyright 2005-2013 uphsh.com. All rights reserved.
 * Support: http://www.uphsh.com
 * Version: 1.0
 */

// 添加Cookie
function addCookie(name, value, options) {
	if (arguments.length > 1 && name != null) {
		if (options == null) {
			options = {};
		}
		if (value == null) {
			options.expires = -1;
		}
		if (typeof options.expires == "number") {
			var time = options.expires;
			var expires = options.expires = new Date();
			expires.setTime(expires.getTime() + time * 1000);
		}
		document.cookie = encodeURIComponent(String(name)) + "=" + encodeURIComponent(String(value)) + (options.expires ? "; expires=" + options.expires.toUTCString() : "") + (options.path ? "; path=" + options.path : "") + (options.domain ? "; domain=" + options.domain : ""), (options.secure ? "; secure" : "");
	}
}

// 获取Cookie
function getCookie(name) {
	if (name != null) {
		var value = new RegExp("(?:^|; )" + encodeURIComponent(String(name)) + "=([^;]*)").exec(document.cookie);
		return value ? decodeURIComponent(value[1]) : null;
	}
}

// 移除Cookie
function removeCookie(name, options) {
	addCookie(name, null, options);
}

// 货币格式化
function currency(value, showSign, showUnit) {
	if (value != null) {
		var price;
		if (setting.priceRoundType == "roundHalfUp") {
			price = (Math.round(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
		} else if (setting.priceRoundType == "roundUp") {
			price = (Math.ceil(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
		} else {
			price = (Math.floor(value * Math.pow(10, setting.priceScale)) / Math.pow(10, setting.priceScale)).toFixed(setting.priceScale);
		}
		if (showSign) {
			price = setting.currencySign + price;
		}
		if (showUnit) {
			price += setting.currencyUnit;
		}
		return price;
	}
}


(function($) {
	$.fn.moneyInput = function(options) {
		this.each(function() {
			var reg=new RegExp(",","g");
			var s = this.value.replace(/\s/g,'').replace(reg,''); 
			if(s){
				s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";//更改这里n数也可确定要保留的小数位  
				var l = s.split(".")[0].split("").reverse(),  
				r = s.split(".")[1];  
				t = "";  
				for(i = 0; i < l.length; i++ )  
				{  
					t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
				}  
				$(this).val(t.split("").reverse().join("") + "." + r.substring(0,2));
			}
			$(this).bind("cut copy paste", function(e) {  
                e.preventDefault();  
            });  
			
			$(this).bind('blur',function(e){
				var reg=new RegExp(",","g");
				var s = this.value.replace(/\s/g,'').replace(reg,''); 
				if(s){
					s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(2) + "";//更改这里n数也可确定要保留的小数位  
					var l = s.split(".")[0].split("").reverse(),  
					r = s.split(".")[1];  
					t = "";  
					for(i = 0; i < l.length; i++ )  
					{  
						t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");  
					}  
					$(this).val(t.split("").reverse().join("") + "." + r.substring(0,2));
				}
			});
		});

		return this;
	};

	
	var zIndex = 100;
	
	// 消息框
	var $message;
	var messageTimer;
	$.message = function() {
		var message = {};
		if ($.isPlainObject(arguments[0])) {
			message = arguments[0];
		} else if (typeof arguments[0] === "string" && typeof arguments[1] === "string") {
			message.type = arguments[0];
			message.content = arguments[1];
		}else if (typeof arguments[0] === "string" && !arguments[1] ) {
			message.type = "tips";
			message.content = arguments[0];
		}  else {
			return false;
		}
		if (message.type == null || !message.content ) {
			return false;
		}
		
//		if ($message == null) {
//			$message = $('<div class="hshMessage"><div class="messageContent message' + message.type + 'Icon"><\/div><\/div>');
//			if (!window.XMLHttpRequest) {
//				$message.append('<iframe class="messageIframe"><\/iframe>');
//			}
//			$message.appendTo("body");
//		}
		
//		$message.children("div").removeClass("messagewarnIcon messageerrorIcon messagesuccessIcon").addClass("message" + message.type + "Icon").html(message.content);
//		$message.css({"margin-left": - parseInt($message.outerWidth() / 2), "z-index": zIndex ++}).show();
		
		if ($message == null) {
			$message = $('<div class="hshMessage"><\/div>').appendTo("body");
		}
		
		$message.html(message.content);
		$message.css({"margin-left": - parseInt($message.outerWidth() / 2),"margin-top": - parseInt($message.outerHeight() / 2),"top": parseInt($(window).height() / 2), "z-index": zIndex ++}).show();
	
		
		clearTimeout(messageTimer);
		messageTimer = setTimeout(function() {
			$message.hide();
		}, 3000);
		return $message;
	}

	// 对话框
	$.dialog = function(options) {
		var settings = {
			width: 320,
			height: "auto",
			modal: true,
			ok: "OK",
			cancel: "Cancel",
			onShow: null,
			onClose: null,
			onOk: null,
			onCancel: null
		};
		$.extend(settings, options);
		
		if (settings.content == null) {
			return false;
		}
		
		var $dialog = $('<div class="dialog" id="dialog"><\/div>');
		var $dialogTitle;
		//var $dialogClose = $('<div class="dialogClose"><\/div>').appendTo($dialog);
		var $dialogContent;
		var $dialogBottom;
		var $dialogOk;
		var $dialogCancel;
		var $dialogOverlay;
		if (settings.title != null) {
			$dialogTitle = $('<div class="dialogTitle"><\/div>').appendTo($dialog);
		}
		if (settings.type != null) {
			$dialogContent = $('<div class="dialogContent dialog' + settings.type + 'Icon"><\/div>').appendTo($dialog);
		} else {
			$dialogContent = $('<div class="dialogContent"><\/div>').appendTo($dialog);
		}
		if (settings.ok != null || settings.cancel != null) {
			$dialogBottom = $('<div class="dialogBottom"><\/div>').appendTo($dialog);
		}
		if (settings.ok != null) {
			$dialogOk = $('<input type="button" class="button" value="' + settings.ok + '" \/>').appendTo($dialogBottom);
		}
		if (settings.cancel != null) {
			$dialogCancel = $('<input type="button" class="button" value="' + settings.cancel + '" \/>').appendTo($dialogBottom);
		}
		if (!window.XMLHttpRequest) {
			$dialog.append('<iframe class="dialogIframe"><\/iframe>');
		}
		$dialog.appendTo("body");
		if (settings.modal) {
			$dialogOverlay = $('<div class="dialogOverlay" id="dialogOverlay"><\/div>').insertAfter($dialog);
		}
		
		var dialogX;
		var dialogY;
		if (settings.title != null) {
			$dialogTitle.text(settings.title);
		}
		$dialogContent.append($('<img alt="123" src="images/warn.png" width="60%">'));
		$dialogContent.append($('<br>'));
		$dialogContent.append(settings.content);
		$dialog.css({"width": settings.width, "height": settings.height, "margin-left": - parseInt(settings.width / 2), "z-index": zIndex ++});
		dialogShow();
		
		if ($dialogTitle != null) {
			$dialogTitle.mousedown(function(event) {
				$dialog.css({"z-index": zIndex ++});
				var offset = $(this).offset();
				if (!window.XMLHttpRequest) {
					dialogX = event.clientX - offset.left;
					dialogY = event.clientY - offset.top;
				} else {
					dialogX = event.pageX - offset.left;
					dialogY = event.pageY - offset.top;
				}
				$("body").bind("mousemove", function(event) {
					$dialog.css({"top": event.clientY - dialogY, "left": event.clientX - dialogX, "margin": 0});
				});
				return false;
			}).mouseup(function() {
				$("body").unbind("mousemove");
				return false;
			});
		}
		
//		if ($dialogClose != null) {
//			$dialogClose.click(function() {
//				dialogClose();
//				return false;
//			});
//		}
		
		if ($dialogOk != null) {
			$dialogOk.click(function() {
				if (settings.onOk && typeof settings.onOk == "function") {
					if (settings.onOk($dialog) != false) {
						dialogClose();
					}
				} else {
					dialogClose();
				}
				return false;
			});
		}
		
		if ($dialogCancel != null) {
			$dialogCancel.click(function() {
				if (settings.onCancel && typeof settings.onCancel == "function") {
					if (settings.onCancel($dialog) != false) {
						dialogClose();
					}
				} else {
					dialogClose();
				}
				return false;
			});
		}
		
		function dialogShow() {
			if (settings.onShow && typeof settings.onShow == "function") {
				if (settings.onShow($dialog) != false) {
					$dialog.show();
					$dialogOverlay.show();
				}
			} else {
				$dialog.show();
				$dialogOverlay.show();
			}
		}
		function dialogClose() {
			if (settings.onClose && typeof settings.onClose == "function") {
				if (settings.onClose($dialog) != false) {
					$dialogOverlay.remove();
					$dialog.remove();
				}
			} else {
				$dialogOverlay.remove();
				$dialog.remove();
			}
		}
		return $dialog;
	};
	
	// 对话框
	$.dialogClose = function(options) {
		$("#dialog").remove();
		$("#dialogOverlay").remove();
	};
})(jQuery);
